USE [HUNGDONG2]
GO

/****** Object:  StoredProcedure [dbo].[sp_NVDangKyLichDiup]    Script Date: 10/13/2013 21:06:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

create proc [dbo].[sp_NVDangKyLichDiup]
@manv varchar(10),
@matuyen int,
@maxe int,
@ngaydi datetime
as
begin tran
	--set tran isolation level Serializable
	--insert into dangkylichdi values(@manv,@matuyen,@maxe,@ngaydi,1);
	declare @maloainv int =(select LoaiNV from NhanVien where MaNV=@manv)
	print @maloainv
	if(@maloainv = 2)
	begin
		declare @sltai int=(select SLTaiXeDaDangKi from LichDi with (Xlock) where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi)
		set @sltai=@sltai+1
		waitfor delay '0:0:05'
		update LichDi 
		set SLTaiXeDaDangKi=@sltai
		where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi
	end
	else
	begin
		declare @slphu int=(select SLPhuLaiDaDangKi from LichDi with (Xlock) where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi)
		set @slphu=@slphu+1
		waitfor delay '0:0:05'
		update LichDi 
		set SLPhuLaiDaDangKi=@slphu
		where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi
	end
	
commit tran


GO


--
CREATE proc [dbo].[sp_huylichdidadangky]
@manv varchar(10),
@matuyen int,
@maxe int,
@ngaydi datetime
as
begin
	delete from DangKyLichDi where manv=@manv and matuyen=@matuyen and maxe=@maxe and ngaydi=@ngaydi
	
end


GO

--

CREATE proc [dbo].[sp_huylichdidadangkyup]
@manv varchar(10),
@matuyen int,
@maxe int,
@ngaydi datetime
as
begin tran
	--delete from DangKyLichDi where manv=@manv and matuyen=@matuyen and maxe=@maxe and ngaydi=@ngaydi
		declare @maloainv int =(select LoaiNV from NhanVien where MaNV=@manv)
	print @maloainv
	if(@maloainv = 2)
	begin
		declare @sltai int=(select SLTaiXeDaDangKi from LichDi with(Xlock) where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi)
		set @sltai=@sltai-1
		waitfor delay '0:0:05'
		update LichDi
		set SLTaiXeDaDangKi=@sltai
		where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi
	end
	else
	begin
		declare @slphu int=(select SLPhuLaiDaDangKi from LichDi with(Xlock) where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi)
		set @slphu=@slphu-1
		waitfor delay '0:0:05'
		update LichDi
		set SLPhuLaiDaDangKi=@slphu
		where MaTuyen=@matuyen and MaXe=@maxe and NgayDi=@ngaydi
	end
commit


GO



